package gameOfLife;

import java.util.ArrayList;

import lib.Location;

/**
 * Helper function to find the neighbors of a Locaiton
 *
 */
public class LocationHelperImpl{ 
	/**
	 * Returns an array of valid locations, omitting the ones 
	 * that are out of bounds.
	 */
	private Location[] verifyLocation(ArrayList<Location> loc,int dimX, int dimY){
		ArrayList<Location> new_locs = new ArrayList<Location>();
		for(Location l :loc){
			if(l.getX()>=0 && l.getX()<dimX && l.getY()>=0 && l.getY()<dimY){
				new_locs.add(l);
			}
		}
		return (Location[])(new_locs.toArray(new Location[0]));
	}
	
	/**
	 * Returns an array of valid neighbors which are adjacent to
	 * the loc parameter
	 * @return
	 */
	public Location[] getNeighbors(Location loc,int dimX, int dimY) {
		int X = loc.getX();
		int Y = loc.getY();
		ArrayList<Location> neighbors = new ArrayList<Location>();
		for(int i = X - 1; i <= X + 1; i++){
			for(int j = Y - 1; j <= Y + 1; j++){
				if(!(i == X && j == Y))
					neighbors.add(new Location(i,j));					
			}
		}
		return verifyLocation(neighbors, dimX, dimY);
	}
	
}
